package com.intelligent.bot.api;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.bot.base.exception.E;
import com.intelligent.bot.base.result.B;
import com.intelligent.bot.model.Classify;
import com.intelligent.bot.model.Team;
import com.intelligent.bot.model.base.BaseDeleteEntity;
import com.intelligent.bot.model.req.*;
import com.intelligent.bot.model.res.ClassifyListRes;
import com.intelligent.bot.service.IClassifyService;
import com.intelligent.bot.service.ITeamService;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/team")
@Log4j2
@Transactional(rollbackFor = E.class)
public class TeamApi {

    @Resource
    ITeamService teamService;

    @Resource
    IClassifyService classifyService;

    @RequestMapping(value = "list",name = "列表")
    public B<Page<Team>> list(@Validated @RequestBody TeamGetListReq req)  {
        return teamService.list(req);
    }

    @RequestMapping(value = "add",name = "添加")
    public B<Void> add(@Validated @RequestBody TeamAddReq req)  {
        return teamService.add(req);
    }


    @RequestMapping(value = "/update",name = "修改")
    public B<Void> update(@Validated @RequestBody TeamUpdateReq req)  {
        return teamService.update(req);
    }

    @RequestMapping(value = "delete",name = "删除")
    public B<Void> delete(@Validated @RequestBody BaseDeleteEntity req)  {
        return teamService.delete(req);
    }

    @RequestMapping(value = "/allocation",name = "分配分类关联")
    public B<Void> allocation(@Validated @RequestBody TeamAllocationReq req)  {
        return teamService.allocation(req);
    }

    @RequestMapping(value = "/getClassifyList",name = "查询关联分类信息")
    public B<List<Classify>> getClassifyList(@Validated @RequestBody GetClassifyListReq req)  {
        return teamService.getClassifyList(req);
    }

    @RequestMapping(value = "/classifyList",name = "查询分类列表")
    public B<List<ClassifyListRes>> classifyList()  {
        return B.okBuild(buildTree(BeanUtil.copyToList(classifyService.list(), ClassifyListRes.class)));
    }
    public static List<ClassifyListRes> buildTree(List<ClassifyListRes> categories) {
        // 首先将所有节点放入一个 Map 中，便于快速查找
        Map<Long, ClassifyListRes> categoryMap = new HashMap<>();
        for (ClassifyListRes category : categories) {
            categoryMap.put(category.getId(), category);
        }

        // 构建树形结构
        List<ClassifyListRes> rootCategories = new ArrayList<>();
        for (ClassifyListRes category : categories) {
            Long parentId = category.getParentId();

            // 如果是顶级节点（parentId 为 0），直接添加到根节点列表
            if (parentId == 0) {
                rootCategories.add(category);
            } else {
                // 否则，将当前节点添加到其父节点的子节点列表中
                ClassifyListRes parent = categoryMap.get(parentId);
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(category);
                }
            }
        }

        return rootCategories;
    }
}
